import 'package:dotted_border/dotted_border.dart';
import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import '../../resources/kq_theme_colors.dart';

/// 标签控件
///
/// 圆角边框(实线或虚线) + 文字
///
/// @author 周卓
///
class KqTagView extends StatelessWidget {
  /// 文字
  final String text;

  /// 字体大小，默认14
  final double? fontSize;

  /// 内边距
  final EdgeInsets? padding;

  /// 是否虚线，默认false
  final bool dashLine;

  /// 文字颜色，默认黄色
  final Color textColor;

  /// 边框颜色，默认黄色
  final Color borderColor;

  /// 背景颜色，默认透明
  final Color backgroundColor;

  /// 圆角，默认3
  final double? radius;

  /// 最大宽度，默认68
  final double? maxWidth;

  /// 标签高度，不传则高度自适应
  final double? height;

  const KqTagView({
    super.key,
    required this.text,
    this.padding,
    this.dashLine = false,
    this.fontSize,
    this.maxWidth,
    this.textColor = KqThemeColors.textYellow,
    this.borderColor = KqThemeColors.textYellow,
    this.backgroundColor = KqThemeColors.bgTransparent,
    this.radius,
    this.height,
  });

  @override
  Widget build(BuildContext context) {
    Widget result = Text(
      text,
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
      style: TextStyle(fontSize: fontSize ?? 12.sp, color: textColor),
    );

    if (height != null) {
      // 垂直居中，水平方向根据child宽度自适应，不传widthFactor: 1则会充满父组件最大宽度
      result = Align(
        alignment: Alignment.centerLeft,
        widthFactor: 1,
        child: result,
      );
    }

    result = Container(
      color: dashLine ? backgroundColor : null,
      padding:
          padding ?? EdgeInsets.symmetric(horizontal: 3.5.r, vertical: 2.5.r),
      constraints: BoxConstraints(
        maxWidth: maxWidth ?? 68.r,
        minHeight: height ?? 0.0,
        maxHeight: height ?? double.infinity,
      ),
      decoration: dashLine
          ? null
          : BoxDecoration(
              color: backgroundColor,
              borderRadius: BorderRadius.all(Radius.circular(radius ?? 2.r)),
              border: Border.all(width: 0.5, color: borderColor)),
      child: result,
    );

    if (dashLine) {
      result = DottedBorder(
        borderType: BorderType.RRect,
        strokeWidth: 0.5,
        padding: const EdgeInsets.all(0.5),
        color: borderColor,
        borderPadding: const EdgeInsets.only(bottom: 0.5),
        radius: Radius.circular(radius ?? 2.r),
        child: ClipRRect(
          borderRadius: BorderRadius.all(Radius.circular(radius ?? 2.r)),
          child: result,
        ),
      );
    }

    return result;
  }
}
